<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in">
      <div class="cornerComment-in-title">
        <span>区角评分规则</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-divider>评分规则</el-divider>
        <el-form label-width="0px" ref="editForm">
          <el-form-item v-if="addAssets==1">
            <v-selectpage :data="selectpagelist" key-field="id" show-field="title" class="form-control"
                          :tb-columns="columns" :multiple="true" v-model="domain_ids" @values="multipleValues"
                          placeholder="选择评分规则" title="选择评分规则" :page-size="10" ref="selectPoint" style="width: 100%;">
            </v-selectpage>
            <div class="point-body">
              <div class="point-list" v-for="(item,index) in multipleValuesList">
                <div class="point-list-title">
                  <p class="point-list-title-left">【{{ index + 1 }}】{{ item.title }}</p>
                  <v-selectpage :data="scoreList" key-field="id" show-field="name" class="form-control" :multiple="true"
                                v-model="item.scoreValue" @values="scoreListFn($event,index)" placeholder="选择评分等级"
                                title="设置等级及分值"></v-selectpage>
                </div>
                <div class="point-list-line">
                  <div class="point-list-line-list" v-for="(pointItem,pointIndex) in item.scoreList">
                    <span class="point-list-line-list-title">{{ pointItem.name }}</span>
                    <div class="point-list-line-list-input">
                      <el-input v-model="pointItem.scoreValue" auto-complete="off" placeholder="设定分值"
                                size="mini"></el-input>
                      分
                    </div>
                    <div class="point-list-line-list-input-desc">
                      <el-input v-model="pointItem.desc" auto-complete="off" placeholder="填写描述" size="mini"
                                type="textarea" rows="2"></el-input>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </el-form-item>
          <el-form-item v-if="addAssets==2">
            <div class="point-body">
              <div class="point-list" v-for="(item,index) in assessList" :key="index">
                <div class="point-list-title">
                  <p class="point-list-title-left" @click="getLinebox(index)">【{{ index + 1 }}】{{ item.assess.title }}</p>
                  <span class="point-list-title-right" @click="getLinebox(index)">{{ item.status ? '收起' : '展开' }}</span>
                </div>
                <div class="point-list-line" v-if="item.status">
                  <div class="point-list-line-list" v-for="(pointItem,pointIndex) in item.fraction" :key="pointItem.id">
                    <span class="point-list-line-list-title">{{ pointItem.name }}</span>
                    <div class="point-list-line-list-input">
                      {{ pointItem.score }}分
                    </div>
                    <div class="point-list-line-list-input-desc desc-be">
                      {{ pointItem.desc }}
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </el-form-item>
        </el-form>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click.native="closePOpup" size="small">取消</el-button>
        <el-button size="small" type="primary" @click="checkEditFn">确定并保存</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {cornerCateDetail, cornerCateAssessAdd, cornerCateAssessEdit} from '@/api/admin/corner'

export default {
  props: ['visible', 'cateId'], // recruitmentType examine 审核
  components: {},
  data() {
    return {
      dialogVisible: false,
      // domain_ids
      domain_ids: '',
      columns: [
        {title: '区角评价维度', data: 'title'},
        {
          title: '领域关联',
          data: function (row) {
            let string_ = []
            for (let i in row.domain_ids) {
              string_.push(row.domain_ids[i].name)
            }
            return string_.join(',')
          }
        },
        {title: '描述', data: 'description'}
      ],
      multipleValuesList: [],
      newList: {},
      scoreList: [], // 评分分列表
      selectpagelist: [],
      assessList: [],
      cornerId: 0,
      addAssets: 0 // 1 添加 2 编辑
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible) {
        if (this.cateId) {
          this.getDetail()
          this.getFielFn(); //
          this.getPointFn()
        }
      } else {
        this.multipleValuesList = []
        this.newList = {}
        this.scoreList = [] // 评分分列表
        this.selectpagelist = []
        this.assessList = []
        this.domain_ids = ''
      }
    }
  },
  created() {

  },
  methods: {
    async checkEditFn() {
      console.log('当前值', this.multipleValuesList)
      let listScore = []
      for (let i in this.multipleValuesList) {
        let newList = {}
        let newDomain = []
        newList.fraction = []
        newList.items_id = this.multipleValuesList[i].id
        for (let n in this.multipleValuesList[i].domain_ids) {
          newDomain.push(this.multipleValuesList[i].domain_ids[n].id)
        }
        newList.domain_ids = newDomain.join(',')

        if (!this.multipleValuesList[i].scoreValue) {
          this.$message.error(`请选择相应评分等级(${i + 1})`)
          return
        } else {
          for (let n in this.multipleValuesList[i].scoreList) {
            let newScoreList = {}
            if (!this.multipleValuesList[i].scoreList[n].scoreValue) {
              this.$message.error(`请填写评分相应的分值,${i + 1}-${n + 1}`)
              return
            }
            newScoreList.id = this.multipleValuesList[i].scoreList[n].id
            newScoreList.name = this.multipleValuesList[i].scoreList[n].name
            newScoreList.score = this.multipleValuesList[i].scoreList[n].scoreValue
            newScoreList.desc = this.multipleValuesList[i].scoreList[n].desc
            newList.fraction.push(newScoreList)
          }
        }
        listScore.push(newList)
      }
      console.log('领域内容listScore：', listScore)
      if (this.addAssets === 1) {
        console.log('添加')
        cornerCateAssessAdd({cate_id: this.cateId, list: JSON.stringify(listScore)}).then(res => {
          console.log('res:', res)
          if (!res.code) {
            this.$message.success(res.msg)
            this.closePOpup()
          } else {
            this.$message.error(res.msg)
          }
        })
      }
      if (this.addAssets === 2) {
        console.log('编辑')
        cornerCateAssessEdit({cate_id: this.cateId, list: JSON.stringify(listScore)}).then(res => {
          console.log('res:', res)
          if (!res.code) {
            this.$message.success(res.msg)
            this.closePOpup()
          } else {
            this.$message.error(res.msg)
          }
        })
      }
    },
    getDetail() {
      cornerCateDetail({id: this.cateId}).then(res => {
        console.log('res:', res)
        if (res.vo.list && !res.vo.list.length) {
          this.addAssets = 1
        }
        if (res.vo.list && res.vo.list.length) {
          let domain_ids_ = []
          for(let item of res.vo.list){
            item.fraction = JSON.parse(item.fraction)
            domain_ids_.push(item.items_id)
          }
          this.domain_ids = domain_ids_.join(',')
          this.assessList = JSON.parse(JSON.stringify(res.vo.list))
          this.addAssets = 2
        }
      })
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
    getChildValue(option) {
      this.editForm.pic = option
    },
    // 获取领域
    getFielFn() {
      this.$post(this.api.corner.attr.index, {
        p: 0
      }).then((response) => {
        if (response.code === 0) {
          this.selectpagelist = response.list
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    getPointFn() {
      this.$post(this.api.setting.cornerIndex, {
        p: 1
      }).then((response) => {
        if (response.code === 0) {
          this.scoreList = response.list.data
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    multipleValues(option) {
      let newArray = {}
      for (let i in option) {
        if (this.newList[option[i].id]) {
          option[i].scoreList = this.getJson(this.newList[option[i].id].scoreList)
          option[i].scoreValue = this.getJson(this.newList[option[i].id].scoreValue)
          option[i].desc = this.getJson(this.newList[option[i].id].desc)
        } else {
          option[i].scoreList = []
          option[i].scoreValue = ''
          option[i].desc = ''
        }
        newArray[option[i].id] = option[i] // 对比参数
      }
      this.newList = this.getJson(newArray)
      this.multipleValuesList = this.getJson(option)
    },
    // 分值更改
    scoreListFn(option, index) {
      // 不管添加删除，只要更改了就会变化 option
      let orderDetail_ = JSON.parse(JSON.stringify(this.multipleValuesList));
      let list = this.getJson(this.newList);
      let scoreList = orderDetail_[index].scoreList
      let score_id = []
      for (let i in option) {
        option[i].scoreValue = ''
        // option[i].desc = orderDetail_[index].description
        score_id.push(option[i].id)
        for (let item of scoreList) {
          if (item.id === option[i].id) {
            option[i].scoreValue = item.scoreValue
            option[i].desc = item.desc
          }
        }
      }
      ;

      // 需要判断是否已经存在内容
      orderDetail_[index].scoreList = JSON.parse(JSON.stringify(option));
      list[orderDetail_[index].id].scoreList = JSON.parse(JSON.stringify(option));
      list[orderDetail_[index].id].scoreValue = score_id.join(',')
      this.newList = this.getJson(list);
      this.multipleValuesList = Object.assign({}, this.multipleValuesList, orderDetail_);
      console.log('option:', option, this.newList, this.multipleValuesList[index]);
    },
    getLinebox(index) {
      let list_ = JSON.parse(JSON.stringify(this.assessList[index]))
      list_.status = !list_.status
      this.$set(this.assessList, index, list_)
    },
    // getNewVal() {
    //   this.$emit('getModleVal', {data: this.getJson(this.multipleValuesList), type: 'multipleValuesList'})
    // }
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 700px;
  height: 700px;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.resruitment-title {
  padding: 5px 0;
  border-bottom: 2px solid #abd3f3;
  margin-bottom: 20px;
}

.el-radio {
  margin-right: 20px;
}

.jobTime {
  display: flex;
  align-items: center;
}

.jobTime span {
  padding: 0 10px;
}

.point-list-line-list-input {
  width: 200px;
  margin-right: 20px;
}

.point-list-line-list-input-desc {
  flex: 1;
  padding: 4px 0;
}

.desc-be {
  line-height: 1.3;
}
.point-list-title{
  cursor: pointer;
  border-bottom: 1px solid #bdbdbd;
}
</style>


